package com.smartwebx.ha.biz.assign.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaAreaExample;
import com.smartwebx.ha.dal.assign.bean.HaUserExample;
import com.smartwebx.ha.dal.assign.dao.HaAreaMapper;
import com.smartwebx.ha.dal.assign.dao.HaUserMapper;
import com.smartwebx.ha.dal.usebean.GisAreaInfo;
import com.smartwebx.ha.dal.util.CommonsUtil;
import com.smartwebx.ha.dal.util.MyFunctions;
import com.smartwebx.ha.util.BasicTreeNode;

@Service
public class HaAreaManagerImpl implements HaAreaManager {

	@Autowired
	private HaAreaMapper haAreaDao;
	@Autowired
	private HaUserMapper hauserDao;
	@Autowired
	private HaAreaMapper haAreaMapper;
	@Override
	public List<HaArea> findAll() {
		HaAreaExample haAreaExample = new HaAreaExample();
		haAreaExample.setOrderByClause("id desc");
		List<HaArea> allAreas = haAreaDao.selectByExample(haAreaExample);
		return allAreas;
	}

	@Override
	public HaArea findOne(int areaId) {
		return haAreaDao.selectByPrimaryKey(areaId);
	}

	@Override
	public boolean delArea(String areaIds) {
		HaAreaExample areaExample = new HaAreaExample();
		HaUserExample userExample = new HaUserExample();
		Iterable<String> ids = Splitter.on(",").trimResults().split(areaIds);
		List<Integer> areaIdList = FluentIterable.from(ids)
				.transform(MyFunctions.parseNumberFunctionFor(Integer.class))
				.toList();
		boolean state = false;
		if (areaIdList.isEmpty()) {
			state = true;
		} else {
			areaExample.createCriteria().andIdIn(areaIdList);
			userExample.createCriteria().andAreaIdIn(areaIdList);
			int state1 = haAreaDao.deleteByExample(areaExample);
			int state2 = hauserDao.deleteByExample(userExample);

			if (state1 >= 1 && state2 >= 1) {
				state = true;
			}
		}

		return state;
	}

	@Override
	public boolean addArea(HaArea area) {
		int state = haAreaDao.insert(area);
		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public boolean updateArea(HaArea area) {
		int state = haAreaDao.updateByPrimaryKeySelective(area);
		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public GisAreaInfo getGisAreaInfoByAreaId(int areaId) {
		HaArea haArea = haAreaDao.selectByPrimaryKey(areaId);
		String cenPoint = haArea.getCenterPoint();
		List<String> cenPointList = Splitter.on(',').splitToList(cenPoint);
		String cenLgtd = cenPointList.get(0);
		String cenLttd = cenPointList.get(1);
		GisAreaInfo gisArea = new GisAreaInfo();
		gisArea.setCenLgtd(cenLgtd);
		gisArea.setCenLttd(cenLttd);
		gisArea.setZoomLevel(haArea.getZoomLevel());
		return gisArea;
	}

	@Override
	public List<HaArea> select(String code) {
		HaAreaExample haAreaExample = new HaAreaExample();
		com.smartwebx.ha.dal.assign.bean.HaAreaExample.Criteria haAreaCriteria = haAreaExample.createCriteria();
		haAreaCriteria.andCodeEqualTo(code);
		return this.haAreaMapper.selectByExample(haAreaExample);
	}

	@Override
	public List<HaArea> getAreaData(String type, String param1) {
		HaAreaExample haAreaExample = new HaAreaExample();
		com.smartwebx.ha.dal.assign.bean.HaAreaExample.Criteria haAreaCriteria = haAreaExample.createCriteria();
		if("code".equals(type)){
			haAreaCriteria.andCodeEqualTo(param1);
		}else if("areaId".equals(type)){
			haAreaCriteria.andParentIdEqualTo(Integer.parseInt(param1));
		}
		List<HaArea> haArea = this.haAreaMapper.selectByExample(haAreaExample);
		return haArea;
	}

	@Override
	public List<BasicTreeNode> getAreaTree() {
		List<HaArea> areaList=this.findAll();
		List<BasicTreeNode> treeNodes= new ArrayList<>();
		for(HaArea area:areaList){
			BasicTreeNode treeNode= new BasicTreeNode(area.getId(), area.getParentId(), area.getName(), null);
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}

}
